#include "model/Subdiagram.h"
#include "model/Utils.h"
#include <iostream>
#include <cstring>
#include <sstream>
#include <stdlib.h>
#include <math.h>
#include "model/Entity.h"
#include "model/CompositeAttribute.h"
#include "model/Relation.h"
#include "model/Via.h"
#include "model/Attribute.h"
#include "model/Diagram.h"
#include "gui/Dibujar/Defines.h"

Subdiagram::Subdiagram(const std::string & ip, unsigned port, const std::string & name) : Component(name), m_ip(ip), m_port(port)
{
	m_sizeX = 0;
	m_sizeY = 0;
	m_AttributeNumber = 0;
}


Subdiagram::Subdiagram(unsigned sizeX, unsigned sizeY, const std::string & ip, unsigned port, const std::string & name, int orientation)
	: Component(name, orientation), m_ip(ip), m_port(port)
{
	m_sizeX = sizeX;
	m_sizeY = sizeY;
	m_AttributeNumber = 0;
}


Subdiagram::Subdiagram(const Subdiagram & otro) : Component(otro){

	m_sizeX = otro.m_sizeX;
	m_sizeY = otro.m_sizeY;
	m_ip = otro.m_ip;
	m_port = otro.m_port;
	m_AttributeNumber = otro.m_AttributeNumber;
	initializeConnectors();
}


Subdiagram& Subdiagram::operator=(const Subdiagram& otro) {

	Component::operator=(otro);
	m_sizeX = otro.m_sizeX;
	m_sizeY = otro.m_sizeY;
	m_ip = otro.m_ip;
	m_port = otro.m_port;
	m_AttributeNumber = otro.m_AttributeNumber;
	initializeConnectors();
	return *this;
}

void Subdiagram::initializeConnectors() {
	double alto = m_sizeY * PROP_SUBDIAGRAM;
	int distanciaEntradas = round( alto / (m_AttributeNumber+1));

	for (unsigned i = 0; i<m_AttributeNumber; i++)
		addConnector(0,(i+1) * distanciaEntradas, true);
}

void Subdiagram::serialize(XmlWriter & writer_rep, XmlWriter & writer_comp)
{
	Utils utils;
	writer_rep.addRootChild(TARGET_SUBDIAGRAM, "");
	Component::serialize(writer_rep, writer_comp);
	writer_rep.addCurrentNodeProperty(TARGET_SIZE_X, utils.convertToString(m_sizeX).c_str());
	writer_rep.addCurrentNodeProperty(TARGET_SIZE_Y, utils.convertToString(m_sizeY).c_str());

	writer_rep.addCurrentNodeProperty(TARGET_CANT_ENTRADAS, utils.convertToString(m_AttributeNumber).c_str());

}


void Subdiagram::deserialize(XmlReader & reader)
{
	Utils utils;
	Component::deserialize(reader);
	m_AttributeNumber = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_CANT_ENTRADAS));
	m_sizeX = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_SIZE_X));
	m_sizeY = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_SIZE_Y));
	initializeConnectors();
}


void Subdiagram::deserializeToDraw(XmlReader & reader)
{
	Utils utils;
	reader.setCurrentNode(reader.getRoot());
	setName(reader.getCurrentNodeProperty(TARGET_NAME));
	m_sizeX = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_SIZE_X));
	m_sizeY = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_SIZE_Y));
	m_AttributeNumber = utils.convertToUnsigned(reader.getCurrentNodeProperty(TARGET_CANT_ENTRADAS));
	initializeConnectors();
}

void Subdiagram::addToDiagram(Diagram &diagram){
	diagram.addChip(this);
}

void Subdiagram::removeFromDiagram(Diagram &diagram){
	diagram.removeChip(this);
}
